<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/14
 * Time: 18:35
 */

require_once "config.php";
require_once "MyPDO.class.php";
$db = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');

$admin = $_COOKIE['user'];

$tid = $_POST['tid'];
$addMoney = $_POST['money'];


$arr = $db->query("cooperation_billing", "`order` = '$tid'", "Row");
if ($arr['id']) {
    echo json_encode(['status' => 'error', 'msg' => '订单号不存在']);
    exit;
}

if ($_POST['id'] == 'one') {
    LogisticsDummySendRequest($tid);
    echo "成功";
} else if ($_POST['id'] == 'two') {
    $json = TradeFullinfoGetRequest($tid);
    if ($json['status'] == 'success') {
       

        $arr = $db->query("cooperation_billing", "`order` = '$tid'", "Row");
        if (!$arr['id']) {
            $money = $json['money'];

            $update = array();
            $update['note'] = '订单充值--' . $json['data']['trade']['buyer_nick'];
            $update['change'] = $money;
            $update['order'] = $tid;
            $update['user'] = $admin;
            $update['entertime'] = $json['data']['trade']['consign_time'];
            $db->insert("cooperation_billing", $update);
            $sql = "UPDATE `cooperation` SET `money` = `money` + $money WHERE `user` = '$admin'";
            $db->execSql($sql);
        }

        $json['msg'] = '充值成功';
        echo json_encode($json);
    } else {
        $json['msg'] = '充值失败';
        echo json_encode($json);
    }
}


function TradeFullinfoGetRequest($tid)
{
    $url = "http://tao.yidaibei.com/taobao/TradeFullinfoGetRequest.php?shop=猫叮儿&tid=$tid";

    $json = file_get_contents($url);
    $json = json_decode($json, 1);
    if ($json['trade']['status'] == 'TRADE_FINISHED' && $json['trade']['type'] == 'fixed' && $json['trade']['orders']['order'][0]['refund_status'] == 'NO_REFUND') {
        return ["status" => "success", "money" => $json['trade']['payment'], "data" => $json];
    }
    return ["status" => "error", "data" => $json];
}


function LogisticsDummySendRequest($tid)
{
    return file_get_contents("http://tao.yidaibei.com/taobao/LogisticsDummySendRequest.php?shop=猫叮儿&tid=$tid");
}